

package com.dfk.bs.common.exception;


import com.dfk.bs.common.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 *
 *
 */
@RestControllerAdvice
public class RRExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(RRException.class)
	public R handleRRException(RRException e){
		R r=new R();
		r.data("code", e.getCode());
		r.data("message", e.getMessage());
		return r;
	}

	@ExceptionHandler(DataIntegrityViolationException.class)
	public R handleDataIntegrityViolationException(DataIntegrityViolationException e){
		logger.error(e.getMessage(), e);
		return R.error().message("数据不能为空");
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public R handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);

		return R.error().message("该记录已存在,请重新选择");
	}


	@ExceptionHandler(Exception.class)
	public R handleException(Exception e){
		logger.error(e.getMessage(), e);
		return R.error();
	}
}
